package com.itzdm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.dto.DishDto;
import com.itzdm.reggie.entity.Category;
import com.itzdm.reggie.entity.Dish;
import com.itzdm.reggie.service.DishFlavorService;
import com.itzdm.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<Page> page(Integer page, Integer pageSize, String name) {
        log.info("分页查询菜品：{}{}{}", page, pageSize,name);
        Page p = dishService.pageDish(page, pageSize,name);
        return Result.success(p);
    }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<String> savaDish(@RequestBody DishDto dishDto) {
        log.info("新增菜品：{}",dishDto.toString());
        dishService.saveWithFlavors(dishDto);
        return Result.success("添加成功");
    }

    /**
     * 批量起售或停售菜品接口
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("批量起售或停售菜品接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status,String ids){
        log.info("{}{}",ids,status);
        dishService.startOrStop(ids,status);
        return Result.success("修改成功");
    }


    /**
     * 批量删除菜品接口
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品接口：{}",ids);
        dishService.deleteByIds(ids);
        return Result.success("删除成功");
    }


    /**
     * 根据id获得菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获得菜品")

    public Result<DishDto> getDishById(@PathVariable Long id) {
        log.info("根据id获得菜品：{}",id);
        DishDto dishDto = dishService.getByIdWithCate(id);
        return Result.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品接口")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<String> updateWithFlavors(@RequestBody DishDto dishDto) {
        log.info("修改菜品：{}",dishDto.toString());
        dishService.updateWithFlavors(dishDto);
        return Result.success("修改成功");
    }
    // 根据分类id获得菜品
@GetMapping("/list")
    @ApiOperation("根据分类id获得菜品")
    // 使用缓存，根据分类id作为缓存key
    @Cacheable(cacheNames = "dishCache",key = "#dish.categoryId")
    public Result<List<DishDto>> getDishesByCategory(Dish dish) {
        // 打印日志
        log.info("根据分类id获得菜品：{}",dish);
        // 调用service获取菜品列表
        List<DishDto> list = dishService.getDishesByCategory(dish);
        // 返回结果
        return Result.success(list);
    }
}
